//加载公共样式
require('../../assets/css/index.less')


const CaptchaMini = require('../../lib/captcha/captcha-mini')

const request = require('../../utils/request')

//字体图标
require('../../assets/fonts/iconfont.css')
//加载自己的样式
require('./forget.less')

window.onload=()=>{
    let phoneIpt = document.querySelector('#phoneIpt')
    let codeIpt = document.querySelector('#codeIpt')
    let pwdIpt = document.querySelector('#pwdIpt')
    let pwdTwiceIpt = document.querySelector('#pwdTwiceIpt')
    let submit = document.querySelector('#submit')
    //验证码
    let captcha1 = new CaptchaMini({
        fontSize: 20,
    });
    let captcha = ''
    captcha1.draw(document.querySelector('#captcha1'), r => {
        captcha = r
        console.log(r, '验证码1');
    });

    //是否存在 存在可修改
    submit.addEventListener('click', function () {
        let errMsg = ''
        if (!/^1[3456789]\d{9}$/.test(phoneIpt.value)) {
            errMsg = '手机号由11位数字组成'
        } else if (codeIpt.value.toLowerCase() !== captcha.toLowerCase()) {
            errMsg = '验证码错误'
        } else if (!/^\w{6,12}$/.test(pwdIpt.value)) {
            errMsg = '密码由6-12位字母或数字组成'
        } else if (pwdIpt.value != pwdTwiceIpt.value) {
            errMsg = '密码不一致'
        } else {
            console.log(111);
            request.post('/api/user/isExist', {
                account: phoneIpt.value
            }).then(res => {
                console.log(res);
                let { errno } = res.data
                if (errno == 0) {
                    request.post('/api/user/register', {
                        account: phoneIpt.value,
                        password: pwdIpt.value
                    }).then(res => {
                        console.log(res);
                        let { errno, message } = res.data
                        if (errno == 0) {
                            location.href = './login.html'
                        } else {
                            alert(message)
                        }
                    })
                } else {
                    alert('该账号已存在')
                }
            })
        }
        if (errMsg) {
            alert(errMsg)
        }
    })
}